package com.wokeMy.view.html;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.woke.MainActivity;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

public class RhkjzfHtmlActivity extends AppCompatActivity {
    WebView mWebView;
    private String url, urlname;
    private Intent intent;
    //	public static String TEST_IMAGE;
    //	private static final String FILE_NAME = "/ic_icon2.jpg";
    private ClipData myClip;
    private ClipboardManager myClipboard;

    private Handler handler;

    Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rhkjzf_html);
        dialog= Util.createLoadingDialog(RhkjzfHtmlActivity.this);
        // 获取程序中的WebView组件
        mWebView = (WebView) findViewById(R.id.webView1_rhkjizfhtml_wv);
        findViewById(R.id.back_rhkjzfHtml_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RhkjzfHtmlActivity.this,
                        MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });
         url=getIntent().getStringExtra("rhkjzfhtml");
        Log.e("rhkjzfhtml",url);
//        StringBuilder sb = new StringBuilder();
//        // 拼接一段HTML代码
//        sb.append("<html>");
//        sb.append("<head>");
//        sb.append("<title> 欢迎您 </title>");
//        sb.append("</head>");
//        sb.append("<body>");
//        sb.append("<h2> 欢迎您访问<a href=\"http://www.cctv.com\">"
//                + "Java联盟</a></h2>");
//        sb.append("</body>");
//        sb.append("</html>");

        // 使用简单的loadData方法会导致乱码，可能是Android API的Bug
        //  show.loadData(sb.toString() , "text/html" , "utf-8");

        //  加载、并显示HTML代码
        intvkew();
        mWebView.loadDataWithBaseURL(null, url, "text/html" , "utf-8", null);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断是否可以返回操作
        if (mWebView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            mWebView.goBack();

        } else {
            mWebView.destroy();
            finish();
        }
        return true;

    }



    @SuppressLint("JavascriptInterface")
    private void intvkew() {


        myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        WebSettings settings = mWebView.getSettings();
        setSettings(settings);
        mWebView.setWebViewClient(new MyWebViewClient());
//        mWebView.loadDataWithBaseURL(null, url, "text/html" , "utf-8", null);

    }
    private void setSettings(WebSettings settings) {
        settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        mWebView.setBackgroundColor(getResources().getColor(R.color.white)); // 设置背景色
        //		mWebView.getBackground().setAlpha(50); // 设置填充透明度 范围：0-255
        //		settings.setSupportZoom(false);// 用于设置webview放大
        //		settings.setBuiltInZoomControls(false);
        settings.setDomStorageEnabled(true);
        //		settings.setTextSize (TextSize.NORMAL);
        ////		settings.setPluginState(PluginState.ON);
        settings.setDatabaseEnabled(true);
    }


    // 监听
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null) {
                Log.e("UrlLoading",url);
                //                Uri uri = Uri.parse(url);
                //                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                //                startActivity(intent);
                view.loadUrl(url);
            }
            return false;

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            mWebView.setVisibility(View.VISIBLE);
            Util.closeDialog(dialog);
            Log.e("onPageFinishedurl......",url);
            Log.e("onPageFinishedgetUrl",view.getUrl());
            //            Log.e("getOriginalUrl()",view.getOriginalUrl());
            handler = new Handler();
            if(url.contains("status=00")||view.getUrl().contains("status=00")){
                // 延迟SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                    }
                }, 1000);
            }else if (url.contains("bind_front")||view.getUrl().contains("bind_front")){
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        finish();
                    }
                }, 1000);
            }
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.getSettings().setJavaScriptEnabled(true);
            Log.e("onPageStarted",url);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(null, 1, "访问失败", null);
            Log.e("onReceivedError......",failingUrl);
        }
    }



}
